//
//  EeventDetailsViewController.m
//  Remind
//
//  Created by macZF on 2017/10/28.
//  Copyright © 2017年 2017.09.27. All rights reserved.
//

#import "EeventDetailsViewController.h"
#import "DataCach.h"

@interface EeventDetailsViewController ()

@end

@implementation EeventDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveData)];
    //
    _eventName.text = _key;
    _intervalTime.text = _timeStr;
    
    NSString *str = [DataCach getValueByClassName:_key];
    [DataCach addTextFieldInputAccessoryView:_describeText];
    _describeText.text = str?str:@"添加描述！！！";
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDeleteImgBtn:)];
    [_deleteImgBtn addGestureRecognizer:tap];
}

- (void)tapDeleteImgBtn:(UITapGestureRecognizer *)sender {
    
    UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:@"提示" message:@"确认删除该事件" preferredStyle:UIAlertControllerStyleAlert];
                                   
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:[DataCach getValueByClassName:_className]];
        [dict removeObjectForKey:_key];
        
        BOOL ok = [DataCach setObject:dict forClassName:_className IfTheDataIsNullInitialized:[NSMutableDictionary class] ReplaceData:YES];
        BOOL right = [DataCach removeValueByClassName:_key];
        if(ok && right){
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"reFreshTableView" object:_className];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [alertCon addAction:okAction];
    [alertCon addAction:cancelAction];
    [self presentViewController:alertCon animated:YES completion:nil];
}

- (void)saveData{
    BOOL ok = [DataCach setObject:_describeText.text forClassName:_key IfTheDataIsNullInitialized:[NSString class] ReplaceData:YES];
    if(ok) [self.navigationController popViewControllerAnimated:YES];
}

- (void)done{
    [_describeText resignFirstResponder];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_describeText resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
